package com.xhwl.logistics.strategy.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.cent.auth.AuthAccountMapping;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.manipulate.CreateVehicleCarBO;
import com.xhwl.logistics.bo.manipulate.DeleteVehicleCarBO;
import com.xhwl.logistics.bo.manipulate.UpdateVehicleCarBO;
import com.xhwl.logistics.dto.http.*;
import com.xhwl.logistics.dto.http.query.DefinePeopleDaHuaDTO;
import com.xhwl.logistics.entity.*;
import com.xhwl.logistics.enums.VehicleCardTypeEnum;
import com.xhwl.logistics.service.cent.IAccountService;
import com.xhwl.logistics.service.cent.IAuthAccountService;
import com.xhwl.logistics.service.impl.*;
import com.xhwl.logistics.service.impl.http.DaHuaHttpServiceImpl;
import com.xhwl.logistics.service.impl.http.HongMenHttpServiceImpl;
import com.xhwl.logistics.strategy.IVehicleParkingLotCarAuthHandler;
import com.xhwl.logistics.util.AboutDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;


/**
 * 调用大华icc业务服务来实现车辆的授权
 */
@Slf4j
//@Service
public class VehicleParkingLotDaHuaIccCarAuthHandlerImpl implements IVehicleParkingLotCarAuthHandler {

    /**
     * 一些大华车行的默认值
     */
    private static final String deptId = "1";
    private static final String carBrand="-1";
    private static final String carColor="99";
    private static final String totalParkCur="1";
    private static final String deptName = "根部门";
    private static final String certificateType="111";
    private static final String cardNumber="";
    private static final String money="0";
    private static final String carStatus = "1";
    private static final Integer parkType = 1;

    @Autowired
    private DaHuaHttpServiceImpl daHuaHttpService;

    @Autowired
    private IAuthAccountService authAccountService;

    @Autowired
    private VehicleAreaServiceImpl vehicleAreaService;

    @Autowired
    private VehicleParkingLotServiceImpl vehicleParkingLotService;

    @Autowired
    private VehicleCardServiceImpl vehicleCardService;

    @Autowired
    private IAccountService accountService;

    @Autowired
    private VehicleCarServiceImpl vehicleCarService;

    @Autowired
    private VehicleRelationCarToCardServiceImpl vehicleRelationCarToCardService;

    @Autowired
    private HongMenHttpServiceImpl hongMenHttpService;

    private Wrapper<VehicleArea> conditionForArea(Integer parkId){
        LambdaQueryWrapper<VehicleArea> vehicleAreaLambdaQueryWrapper = new LambdaQueryWrapper<>();
        return vehicleAreaLambdaQueryWrapper.eq(VehicleArea::getParkingLotId,parkId);
    }

    private Wrapper<VehicleParkingLot> conditionForParkingLot(Integer projectId){
        LambdaQueryWrapper<VehicleParkingLot> vehicleParkingLotLambdaQueryWrapper = new LambdaQueryWrapper<>();
        return vehicleParkingLotLambdaQueryWrapper.eq(VehicleParkingLot::getProjectId,projectId);
    }

    private Wrapper<VehicleCar> conditionForCar(Integer projectId, String plateNumber){
        LambdaQueryWrapper<VehicleCar> vehicleCarLambdaQueryWrapper = new LambdaQueryWrapper<>();
        return vehicleCarLambdaQueryWrapper.eq(VehicleCar::getProjectId,projectId)
                .eq(VehicleCar::getPlateNumber,plateNumber);
    }

    private Wrapper<VehicleRelationCarToCard> conditionForCarCard(Integer carId){
        LambdaQueryWrapper<VehicleRelationCarToCard> vehicleRelationCarToCardLambdaQueryWrapper = new LambdaQueryWrapper<>();
        return vehicleRelationCarToCardLambdaQueryWrapper.eq(VehicleRelationCarToCard::getCarId,carId);
    }

    @Override
    public void addCar(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, CreateVehicleCarBO createVehicleCarBO) {
//        CreateVehicleCarBO createVehicleCarBO = JSONObject.parseObject(carDataStr, CreateVehicleCarBO.class);
        //判断是否是免费卡，如果是免费卡并且有效期为空，就弄成有效期为100年
        this.setFreeCardTime(createVehicleCarBO);
        //调用授权中台的feign接口来获取该用户和第三方的联系
        AuthAccountMapping authAccountMapping = authAccountService.query(createVehicleCarBO.getProjectId(), createVehicleCarBO.getOwnerAccountId(), vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId());
        //如果中台用户与第三方用户id存在联系
        if(authAccountMapping!=null){
            //获取该项目下所有的区域信息
            VehicleParkingLot vehicleParkingLot = vehicleParkingLotService.getOne(conditionForParkingLot(vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId()));
            List<VehicleArea> vehicleAreaList = vehicleAreaService.list(conditionForArea(vehicleParkingLot.getId()));
            String parkingLotCode="";
            for (VehicleArea vehicleArea : vehicleAreaList) {
                parkingLotCode=parkingLotCode+vehicleArea.getRemark()+",";
            }

            //由于需求问题，修改车辆授权时的场区授权
            if(StringUtils.isNotEmpty(vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getRemark())){
                parkingLotCode = vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getRemark();
            }

            //获取车卡信息
            VehicleCard vehicleCard = vehicleCardService.getById(createVehicleCarBO.getCardId());
            //获取该人员-车辆信息
            OwnerCarInfoDaHuaIccDTO daHuaOwnerCar = daHuaHttpService.getDaHuaOwnerCar(vehicleIntegratedConfigBO, authAccountMapping.getOpenId());
            //如果人员-车辆信息为空，那么就需要将授权的那条信息删除掉
            if(daHuaOwnerCar==null){
                authAccountService.deleteAccountMapping(authAccountMapping.getProjectId(),authAccountMapping.getAccountId(),
                        authAccountMapping.getOpenId(),authAccountMapping.getProductId());
                //将authAccountMapping置为空，使其新增用户
                authAccountMapping=null;
            }
            else{
                List<OwnerParkingLotParkedInfo> ownerParkingLotParkedCurs1 = daHuaOwnerCar.getOwnerVO().getOwnerParkingLotParkedCurs();
                //判断是否是未定义人员，如果为空表示是未定义人员
                if(ownerParkingLotParkedCurs1.isEmpty()){
                    List<DefinePeopleDaHuaDTO> definePeopleDaHuaDTOS = new LinkedList<>();
                    DefinePeopleDaHuaDTO definePeopleDaHuaDTO = new DefinePeopleDaHuaDTO();
                    definePeopleDaHuaDTO.setId(daHuaOwnerCar.getOwnerVO().getId());
                    definePeopleDaHuaDTO.setOwner(daHuaOwnerCar.getOwnerVO().getOwner());
                    definePeopleDaHuaDTO.setOwnerType(vehicleCard.getThirdRuleId());
                    definePeopleDaHuaDTO.setParkingLotDtoList(getParkingLotDTO(vehicleParkingLot.getId()));
                    definePeopleDaHuaDTO.setParkTotal(vehicleAreaList.size());
                    if(createVehicleCarBO.getValidStartTime()!=null){
                        definePeopleDaHuaDTO.setValidStartTime(AboutDateUtil.dateToString(createVehicleCarBO.getValidStartTime()));
                    }else{
                        definePeopleDaHuaDTO.setValidStartTime(AboutDateUtil.dateToString(new Date()));
                    }
                    if(createVehicleCarBO.getValidEndTime()!=null){
                        definePeopleDaHuaDTO.setValidEndTime(AboutDateUtil.dateToString(createVehicleCarBO.getValidEndTime()));
                    }else{
                        definePeopleDaHuaDTO.setValidEndTime(AboutDateUtil.dateToString(AboutDateUtil.datePlusDay(new Date(),1)));
                    }
                    LinkedList<String> validate = new LinkedList<>();
                    validate.add(definePeopleDaHuaDTO.getValidStartTime());
                    validate.add(definePeopleDaHuaDTO.getValidEndTime());
                    definePeopleDaHuaDTO.setValidate(validate);
                    definePeopleDaHuaDTOS.add(definePeopleDaHuaDTO);
                    daHuaHttpService.definePeople(vehicleIntegratedConfigBO, definePeopleDaHuaDTOS);
                }
                daHuaOwnerCar = daHuaHttpService.getDaHuaOwnerCar(vehicleIntegratedConfigBO, authAccountMapping.getOpenId());
                //新增车类
                CarVO carVO = new CarVO();
                carVO.setCarNum(createVehicleCarBO.getPlateNumber());
                carVO.setCarColor(carColor);
                carVO.setCarNumColor(convertForDaHuaCarNumColor(createVehicleCarBO.getCarColor()));
                carVO.setCarType(convertForDaHuaCarType(createVehicleCarBO.getCarType()));
                carVO.setParkinglotCode(parkingLotCode);
                carVO.setCarBrand(carBrand);
                carVO.setDeptId(deptId);
                carVO.setDeptName(deptName);
                carVO.setCarStatus(carStatus);
                carVO.setParkType(parkType);
                //查询是否有这个卡类型了
                List<OwnerParkingLotParkedInfo> ownerParkingLotParkedCurs = daHuaOwnerCar.getOwnerVO().getOwnerParkingLotParkedCurs();
                if(!ownerParkingLotParkedCurs.isEmpty()){
                    OwnerParkingLotParkedInfo ownerParkingLotParkedInfo = ownerParkingLotParkedCurs.stream().filter(s -> {
                        if (StringUtils.equals(s.getOwnerTypeId(), vehicleCard.getThirdRuleId())) {
                            return true;
                        } else {
                            return false;
                        }
                    }).findAny().orElse(null);
                    if(ownerParkingLotParkedInfo==null){
                        OwnerParkingLotParkedInfo ownerParkingLotParkedInfo1 = new OwnerParkingLotParkedInfo();
                        ownerParkingLotParkedInfo1.setOwnerTypeId(vehicleCard.getThirdRuleId());
                        ownerParkingLotParkedInfo1.setTotalParkCur(String.valueOf(vehicleAreaList.size()));
                        if(createVehicleCarBO.getValidStartTime()!=null){
                            ownerParkingLotParkedInfo1.setValidStartTime(AboutDateUtil.dateToString(createVehicleCarBO.getValidStartTime()));
                        }else{
                            ownerParkingLotParkedInfo1.setValidStartTime(AboutDateUtil.dateToString(new Date()));
                        }
                        if(createVehicleCarBO.getValidEndTime()!=null){
                            ownerParkingLotParkedInfo1.setValidEndTime(AboutDateUtil.dateToString(createVehicleCarBO.getValidEndTime()));
                        }else{
                            ownerParkingLotParkedInfo1.setValidEndTime(AboutDateUtil.dateToString(AboutDateUtil.datePlusDay(new Date(),1)));
                        }
                        ownerParkingLotParkedInfo1.setParkingLotDtoList(getParkingLotDTO(vehicleParkingLot.getId()));
                        daHuaOwnerCar.getOwnerVO().getOwnerParkingLotParkedCurs().add(ownerParkingLotParkedInfo1);
                    }
                    else{
                        //如果卡类型为月卡，则进行缴费
                        if(vehicleCard.getCardType()==1){
                            CardMonthChargeDaHuaDTO cardMonthChargeDaHuaDTO = createVehicleCarBOtoCardMonthChargeDaHuaDTO(authAccountMapping.getOpenId(), createVehicleCarBO,vehicleCard);
                            chargeMonthCard(vehicleIntegratedConfigBO,cardMonthChargeDaHuaDTO);
                            //防止后面又进行缴费
                            vehicleCard.setCardType(-1);
                        }
                    }
                }else{
                    throw new BusinessException("定义人员失败！");
                }
                daHuaOwnerCar.getCarVOList().add(carVO);
                daHuaHttpService.updateOwnerCar(vehicleIntegratedConfigBO,daHuaOwnerCar);
                //如果卡类型为月卡，则进行缴费
                if(vehicleCard.getCardType()==1){
                    CardMonthChargeDaHuaDTO cardMonthChargeDaHuaDTO = createVehicleCarBOtoCardMonthChargeDaHuaDTO(authAccountMapping.getOpenId(), createVehicleCarBO,vehicleCard);
                    chargeMonthCard(vehicleIntegratedConfigBO,cardMonthChargeDaHuaDTO);
                }
            }
        }
        //如果中台用户与第三方用户id不存在联系
        if(authAccountMapping==null){
            //获取该项目下所有的区域信息
            VehicleParkingLot vehicleParkingLot = vehicleParkingLotService.getOne(conditionForParkingLot(vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId()));
            //获取车卡信息
            VehicleCard vehicleCard = vehicleCardService.getById(createVehicleCarBO.getCardId());
            List<VehicleArea> vehicleAreaList = vehicleAreaService.list(conditionForArea(vehicleParkingLot.getId()));
            String parkingLotCode="";
            for (VehicleArea vehicleArea : vehicleAreaList) {
                parkingLotCode=parkingLotCode+vehicleArea.getRemark()+",";
            }

            //由于需求问题，修改车辆授权时的场区授权
            if(StringUtils.isNotEmpty(vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getRemark())){
                parkingLotCode = vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getRemark();
            }

            Account account = accountService.findById(createVehicleCarBO.getOwnerAccountId());
            OwnerCarInfoDaHuaIccDTO ownerCarInfoDaHuaIccDTO = new OwnerCarInfoDaHuaIccDTO();

            OwnerVO ownerVO = new OwnerVO();
            ownerVO.setOwner(account.getName());
            ownerVO.setOwnerCode(DaHuaHttpServiceImpl.generateOpenCode(createVehicleCarBO.getOwnerAccountId(), createVehicleCarBO.getProjectId()));
            ownerVO.setCertificateType(certificateType);
            ownerVO.setIdentityCard(account.getIdCard());
            ownerVO.setOwnerTel(createVehicleCarBO.getOwnerPhone());
            ownerVO.setDeptId(deptId);
            ownerVO.setDeptName(deptName);

            List<OwnerParkingLotParkedInfo> ownerParkingLotParkedInfos = new LinkedList<>();
            OwnerParkingLotParkedInfo ownerParkingLotParkedInfo = new OwnerParkingLotParkedInfo();
            ownerParkingLotParkedInfo.setOwnerTypeId(vehicleCard.getThirdRuleId());
            ownerParkingLotParkedInfo.setTotalParkCur(String.valueOf(vehicleAreaList.size()));
            if(createVehicleCarBO.getValidStartTime()!=null){
                ownerParkingLotParkedInfo.setValidStartTime(AboutDateUtil.dateToString(createVehicleCarBO.getValidStartTime()));
            }else{
                ownerParkingLotParkedInfo.setValidStartTime(AboutDateUtil.dateToString(new Date()));
            }
            if(createVehicleCarBO.getValidEndTime()!=null){
                ownerParkingLotParkedInfo.setValidEndTime(AboutDateUtil.dateToString(createVehicleCarBO.getValidEndTime()));
            }else{
                ownerParkingLotParkedInfo.setValidEndTime(AboutDateUtil.dateToString(AboutDateUtil.datePlusDay(new Date(),1)));
            }
            ownerParkingLotParkedInfo.setParkingLotDtoList(getParkingLotDTO(vehicleParkingLot.getId()));

            ownerParkingLotParkedInfos.add(ownerParkingLotParkedInfo);
            ownerVO.setOwnerParkingLotParkedCurs(ownerParkingLotParkedInfos);

            List<CarVO> carVOS = new LinkedList<>();
            CarVO carVO = new CarVO();
            carVO.setCarNum(createVehicleCarBO.getPlateNumber());
            carVO.setCarColor(carColor);
            carVO.setCarNumColor(convertForDaHuaCarNumColor(createVehicleCarBO.getCarColor()));
            carVO.setCarType(convertForDaHuaCarType(createVehicleCarBO.getCarType()));
            carVO.setParkinglotCode(parkingLotCode);
            carVO.setCarBrand(carBrand);
            carVO.setDeptId(deptId);
            carVO.setDeptName(deptName);
            carVO.setCarStatus(carStatus);
            carVO.setParkType(parkType);
            carVOS.add(carVO);

            ownerCarInfoDaHuaIccDTO.setOwnerVO(ownerVO);
            ownerCarInfoDaHuaIccDTO.setCarVOList(carVOS);

            //调用大华接口，新增人员和车辆信息
            daHuaHttpService.addOwnerCar(vehicleIntegratedConfigBO,ownerCarInfoDaHuaIccDTO);
            //查询大华的车辆列表信息
            List<CarInfoDaHuaDTO> daHuaCarList = daHuaHttpService.getDaHuaCarList(vehicleIntegratedConfigBO);
            CarInfoDaHuaDTO carInfoDaHuaDTO=null;
            if(daHuaCarList!=null){
                carInfoDaHuaDTO = daHuaCarList.stream().filter(s -> {
                    if (StringUtils.equals(s.getCarNum(), createVehicleCarBO.getPlateNumber())) {
                        return true;
                    } else {
                        return false;
                    }
                }).findAny().orElse(null);

            }
            //将新增的第三方的用户id与中台用户id进行关联
            if(carInfoDaHuaDTO!=null){
                authAccountService.save(vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId(),createVehicleCarBO.getOwnerAccountId(),
                        carInfoDaHuaDTO.getOwnerId(),carInfoDaHuaDTO.getOwnerCode(),vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId());
                if(vehicleCard.getCardType()==1){
                    CardMonthChargeDaHuaDTO cardMonthChargeDaHuaDTO = createVehicleCarBOtoCardMonthChargeDaHuaDTO(carInfoDaHuaDTO.getOwnerId(), createVehicleCarBO,vehicleCard);
                    chargeMonthCard(vehicleIntegratedConfigBO,cardMonthChargeDaHuaDTO);
                }
            }
        }

        //查看是否开启了蒙牛红门的双厂商定制化需求
        if(vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getCustomParams() != null){
            JSONObject customParamsObj = JSONObject.parseObject(vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getCustomParams());
            if(customParamsObj.getBoolean("doubleFunContrMn")){
                this.addHongMenCar(vehicleIntegratedConfigBO, createVehicleCarBO, customParamsObj);
            }
        }
    }

    /**
     * 蒙牛红门双厂商添加车辆
     */
    public void addHongMenCar(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, CreateVehicleCarBO createVehicleCarBO, JSONObject customParamsObj){
        VehicleCard vehicleCard = vehicleCardService.getById(createVehicleCarBO.getCardId());
        //如果车类型为免费卡和月卡时就对该车辆进行授权
        if(vehicleCard.getCardType().equals(VehicleCardTypeEnum.FREE.getId()) ||
        vehicleCard.getCardType().equals(VehicleCardTypeEnum.MONTH.getId())){
            //注册该车辆的通行证
            hongMenHttpService.addCar(customParamsObj.getString("host"), createVehicleCarBO.getPlateNumber());
            //修改该月卡通行证的有效时间
            if(createVehicleCarBO.getValidEndTime()!=null){
                hongMenHttpService.editCarTime(customParamsObj.getString("host"), createVehicleCarBO.getPlateNumber(), createVehicleCarBO.getValidEndTime());
            }else{
                hongMenHttpService.editCarTime(customParamsObj.getString("host"), createVehicleCarBO.getPlateNumber(), AboutDateUtil.datePlusDay(new Date(),1));
            }
        }
    }

    @Override
    public void updateCar(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, String carDataStr) {
        UpdateVehicleCarBO updateVehicleCarBO = JSONObject.parseObject(carDataStr, UpdateVehicleCarBO.class);
        //调用授权中台的feign接口来获取该用户和第三方的联系
        AuthAccountMapping authAccountMapping = authAccountService.query(vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId(), updateVehicleCarBO.getOwnerAccountId(), vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId());
        //如果中台用户与第三方用户id存在联系
        if(authAccountMapping!=null){
            //先对之前的车牌号进行删除
            List<CarInfoDaHuaDTO> daHuaCarList = daHuaHttpService.getDaHuaCarList(vehicleIntegratedConfigBO);
            VehicleCar vehicleCar11 = vehicleCarService.getById(updateVehicleCarBO.getId());
            //如果车牌号变了，并且新车牌号已存在时，就直接结束
            if(!StringUtils.equals(vehicleCar11.getPlateNumber(),updateVehicleCarBO.getPlateNumber())){
                CarInfoDaHuaDTO carInfoDaHuaDTO = daHuaCarList.stream().filter(s -> {
                    if (StringUtils.equals(s.getCarNum(), updateVehicleCarBO.getPlateNumber())) {
                        return true;
                    } else {
                        return false;
                    }
                }).findAny().orElse(null);
                if(carInfoDaHuaDTO!=null){
                    throw new BusinessException("车牌号码已存在");
                }
            }
            VehicleRelationCarToCard vehicleRelationCarToCard = vehicleRelationCarToCardService.getOne(conditionForCarCard(vehicleCar11.getId()));
            //获取车卡信息
            VehicleCard vehicleCard = vehicleCardService.getById(vehicleRelationCarToCard.getCardId());
            //获取大华的车辆信息，找出需要删除的车辆的第三方id
            List<CarInfoDaHuaDTO> daHuaCarList11 = daHuaHttpService.getDaHuaCarList(vehicleIntegratedConfigBO);
            if(daHuaCarList11!=null){
                CarInfoDaHuaDTO carInfoDaHuaDTO11 = daHuaCarList11.stream().filter(s -> {
                    if (StringUtils.equals(s.getCarNum(), vehicleCar11.getPlateNumber())) {
                        return true;
                    } else {
                        return false;
                    }
                }).findAny().orElse(null);
                if(carInfoDaHuaDTO11!=null){
                    //根据第三方车id删除车辆信息
                    daHuaHttpService.deleteCar(vehicleIntegratedConfigBO, carInfoDaHuaDTO11.getCarId());
                }
            }
            //获取该项目下所有的区域信息
            VehicleParkingLot vehicleParkingLot = vehicleParkingLotService.getOne(conditionForParkingLot(vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId()));
            List<VehicleArea> vehicleAreaList = vehicleAreaService.list(conditionForArea(vehicleParkingLot.getId()));
            String parkingLotCode="";
            for (VehicleArea vehicleArea : vehicleAreaList) {
                parkingLotCode=parkingLotCode+vehicleArea.getRemark()+",";
            }

            //由于需求问题，修改车辆授权时的场区授权
            if(StringUtils.isNotEmpty(vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getRemark())){
                parkingLotCode = vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getRemark();
            }

            OwnerCarInfoDaHuaIccDTO daHuaOwnerCar = daHuaHttpService.getDaHuaOwnerCar(vehicleIntegratedConfigBO, authAccountMapping.getOpenId());
            //如果人员-车辆信息为空，那么就需要将授权的那条信息删除掉
            if(daHuaOwnerCar==null){
                authAccountService.deleteAccountMapping(authAccountMapping.getProjectId(),authAccountMapping.getAccountId(),
                        authAccountMapping.getOpenId(),authAccountMapping.getProductId());
                //将authAccountMapping置为空，使其新增用户
                authAccountMapping=null;
            }
            else{
                //修改大华数据中的车辆信息
                List<CarVO> carVOList = daHuaOwnerCar.getCarVOList();
                CarVO carVO1 = new CarVO();
                carVO1.setCarNum(updateVehicleCarBO.getPlateNumber());
                carVO1.setCarColor(carColor);
                carVO1.setCarNumColor(convertForDaHuaCarNumColor(updateVehicleCarBO.getCarColor()));
                carVO1.setCarType(convertForDaHuaCarType(updateVehicleCarBO.getCarType()));
                carVO1.setParkinglotCode(parkingLotCode);
                carVO1.setCarBrand(carBrand);
                carVO1.setDeptId(deptId);
                carVO1.setDeptName(deptName);
                carVO1.setCarStatus(carStatus);
                carVO1.setParkType(parkType);
                carVOList.add(carVO1);
                List<OwnerParkingLotParkedInfo> ownerParkingLotParkedCurs = daHuaOwnerCar.getOwnerVO().getOwnerParkingLotParkedCurs();
                if(!ownerParkingLotParkedCurs.isEmpty()){
                    OwnerParkingLotParkedInfo ownerParkingLotParkedInfo = ownerParkingLotParkedCurs.stream().filter(s -> {
                        if (StringUtils.equals(s.getOwnerTypeId(), vehicleCard.getThirdRuleId())) {
                            return true;
                        } else {
                            return false;
                        }
                    }).findAny().orElse(null);
                    if(ownerParkingLotParkedInfo==null){
                        OwnerParkingLotParkedInfo ownerParkingLotParkedInfo1 = new OwnerParkingLotParkedInfo();
                        ownerParkingLotParkedInfo1.setOwnerTypeId(vehicleCard.getThirdRuleId());
                        ownerParkingLotParkedInfo1.setTotalParkCur(String.valueOf(vehicleAreaList.size()));
                        if(vehicleRelationCarToCard.getValidStartTime()!=null){
                            ownerParkingLotParkedInfo1.setValidStartTime(AboutDateUtil.dateToString(vehicleRelationCarToCard.getValidStartTime()));
                        }else{
                            ownerParkingLotParkedInfo1.setValidStartTime(AboutDateUtil.dateToString(new Date()));
                        }
                        if(vehicleRelationCarToCard.getValidEndTime()!=null){
                            ownerParkingLotParkedInfo1.setValidEndTime(AboutDateUtil.dateToString(vehicleRelationCarToCard.getValidEndTime()));
                        }else{
                            ownerParkingLotParkedInfo1.setValidEndTime(AboutDateUtil.dateToString(AboutDateUtil.datePlusDay(new Date(),1)));
                        }
                        ownerParkingLotParkedInfo1.setParkingLotDtoList(getParkingLotDTO(vehicleParkingLot.getId()));
                        daHuaOwnerCar.getOwnerVO().getOwnerParkingLotParkedCurs().add(ownerParkingLotParkedInfo1);
                    }
                }else{
                    //新增卡类型
                    OwnerParkingLotParkedInfo ownerParkingLotParkedInfo = new OwnerParkingLotParkedInfo();
                    ownerParkingLotParkedInfo.setOwnerTypeId(vehicleCard.getThirdRuleId());
                    ownerParkingLotParkedInfo.setTotalParkCur(String.valueOf(vehicleAreaList.size()));
                    if(vehicleRelationCarToCard.getValidStartTime()!=null){
                        ownerParkingLotParkedInfo.setValidStartTime(AboutDateUtil.dateToString(vehicleRelationCarToCard.getValidStartTime()));
                    }else{
                        ownerParkingLotParkedInfo.setValidStartTime(AboutDateUtil.dateToString(new Date()));
                    }
                    if(vehicleRelationCarToCard.getValidEndTime()!=null){
                        ownerParkingLotParkedInfo.setValidEndTime(AboutDateUtil.dateToString(vehicleRelationCarToCard.getValidEndTime()));
                    }else{
                        ownerParkingLotParkedInfo.setValidEndTime(AboutDateUtil.dateToString(AboutDateUtil.datePlusDay(new Date(),1)));
                    }
                    ownerParkingLotParkedInfo.setParkingLotDtoList(getParkingLotDTO(vehicleParkingLot.getId()));
                    daHuaOwnerCar.getOwnerVO().getOwnerParkingLotParkedCurs().add(ownerParkingLotParkedInfo);
                }
                daHuaOwnerCar.setCarVOList(carVOList);
                daHuaHttpService.updateOwnerCar(vehicleIntegratedConfigBO,daHuaOwnerCar);
            }
        }
        //如果中台用户与第三方用户id不存在联系
        if(authAccountMapping==null){
            //先对之前的车牌号进行删除
            VehicleCar vehicleCar11 = vehicleCarService.getById(updateVehicleCarBO.getId());
            //获取大华的车辆信息，找出需要删除的车辆的第三方id
            List<CarInfoDaHuaDTO> daHuaCarList11 = daHuaHttpService.getDaHuaCarList(vehicleIntegratedConfigBO);
            if(daHuaCarList11!=null){
                //如果车牌号变了，并且新车牌号已存在时，就直接结束
                if(!StringUtils.equals(vehicleCar11.getPlateNumber(),updateVehicleCarBO.getPlateNumber())){
                    CarInfoDaHuaDTO carInfoDaHuaDTO = daHuaCarList11.stream().filter(s -> {
                        if (StringUtils.equals(s.getCarNum(), updateVehicleCarBO.getPlateNumber())) {
                            return true;
                        } else {
                            return false;
                        }
                    }).findAny().orElse(null);
                    if(carInfoDaHuaDTO!=null){
                        throw new BusinessException("车牌号码已存在");
                    }
                }
                CarInfoDaHuaDTO carInfoDaHuaDTO11 = daHuaCarList11.stream().filter(s -> {
                    if (StringUtils.equals(s.getCarNum(), vehicleCar11.getPlateNumber())) {
                        return true;
                    } else {
                        return false;
                    }
                }).findAny().orElse(null);
                if (carInfoDaHuaDTO11 != null) {
                    //根据第三方车id删除车辆信息
                    daHuaHttpService.deleteCar(vehicleIntegratedConfigBO, carInfoDaHuaDTO11.getCarId());
                }
            }
            //获取该项目下所有的区域信息
            VehicleParkingLot vehicleParkingLot = vehicleParkingLotService.getOne(conditionForParkingLot(vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId()));

            VehicleCar vehicleCar = vehicleCarService.getById(updateVehicleCarBO.getId());
            VehicleRelationCarToCard vehicleRelationCarToCard = vehicleRelationCarToCardService.getOne(conditionForCarCard(vehicleCar.getId()));
            //获取车卡信息
            VehicleCard vehicleCard = vehicleCardService.getById(vehicleRelationCarToCard.getCardId());
            List<VehicleArea> vehicleAreaList = vehicleAreaService.list(conditionForArea(vehicleParkingLot.getId()));
            String parkingLotCode="";
            for (VehicleArea vehicleArea : vehicleAreaList) {
                parkingLotCode=parkingLotCode+vehicleArea.getRemark()+",";
            }

            //由于需求问题，修改车辆授权时的场区授权
            if(StringUtils.isNotEmpty(vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getRemark())){
                parkingLotCode = vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getRemark();
            }

            Account account = accountService.findById(updateVehicleCarBO.getOwnerAccountId());
            OwnerCarInfoDaHuaIccDTO ownerCarInfoDaHuaIccDTO = new OwnerCarInfoDaHuaIccDTO();

            OwnerVO ownerVO = new OwnerVO();
            ownerVO.setOwner(account.getName());
            ownerVO.setOwnerCode(DaHuaHttpServiceImpl.generateOpenCode(updateVehicleCarBO.getOwnerAccountId(), vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId()));
            ownerVO.setCertificateType(certificateType);
            ownerVO.setIdentityCard(account.getIdCard());
            ownerVO.setOwnerTel(updateVehicleCarBO.getOwnerPhone());
            ownerVO.setDeptId(deptId);
            ownerVO.setDeptName(deptName);

            List<OwnerParkingLotParkedInfo> ownerParkingLotParkedInfos = new LinkedList<>();
            OwnerParkingLotParkedInfo ownerParkingLotParkedInfo = new OwnerParkingLotParkedInfo();
            ownerParkingLotParkedInfo.setOwnerTypeId(vehicleCard.getThirdRuleId());
            ownerParkingLotParkedInfo.setTotalParkCur(String.valueOf(vehicleAreaList.size()));
            if(vehicleRelationCarToCard.getValidStartTime()!=null){
                ownerParkingLotParkedInfo.setValidStartTime(AboutDateUtil.dateToString(vehicleRelationCarToCard.getValidStartTime()));
            }else{
                ownerParkingLotParkedInfo.setValidStartTime(AboutDateUtil.dateToString(new Date()));
            }
            if(vehicleRelationCarToCard.getValidEndTime()!=null){
                ownerParkingLotParkedInfo.setValidEndTime(AboutDateUtil.dateToString(vehicleRelationCarToCard.getValidEndTime()));
            }else{
                ownerParkingLotParkedInfo.setValidEndTime(AboutDateUtil.dateToString(AboutDateUtil.datePlusDay(new Date(),1)));
            }
            ownerParkingLotParkedInfo.setParkingLotDtoList(getParkingLotDTO(vehicleParkingLot.getId()));

            ownerParkingLotParkedInfos.add(ownerParkingLotParkedInfo);
            ownerVO.setOwnerParkingLotParkedCurs(ownerParkingLotParkedInfos);

            List<CarVO> carVOS = new LinkedList<>();
            CarVO carVO = new CarVO();
            carVO.setCarNum(updateVehicleCarBO.getPlateNumber());
            carVO.setCarColor(carColor);
            carVO.setCarNumColor(convertForDaHuaCarNumColor(updateVehicleCarBO.getCarColor()));
            carVO.setCarType(convertForDaHuaCarType(updateVehicleCarBO.getCarType()));
            carVO.setParkinglotCode(parkingLotCode);
            carVO.setCarBrand(carBrand);
            carVO.setDeptId(deptId);
            carVO.setDeptName(deptName);
            carVO.setCarStatus(carStatus);
            carVO.setParkType(parkType);
            carVOS.add(carVO);

            ownerCarInfoDaHuaIccDTO.setOwnerVO(ownerVO);
            ownerCarInfoDaHuaIccDTO.setCarVOList(carVOS);

            //调用大华接口，新增人员和车辆信息
            daHuaHttpService.addOwnerCar(vehicleIntegratedConfigBO,ownerCarInfoDaHuaIccDTO);
            //查询大华的车辆列表信息
            List<CarInfoDaHuaDTO> daHuaCarList1 = daHuaHttpService.getDaHuaCarList(vehicleIntegratedConfigBO);
            CarInfoDaHuaDTO carInfoDaHuaDTO1 = daHuaCarList1.stream().filter(s -> {
                if (StringUtils.equals(s.getCarNum(), updateVehicleCarBO.getPlateNumber())) {
                    return true;
                } else {
                    return false;
                }
            }).findAny().orElse(null);
            //将新增的第三方的用户id与中台用户id进行关联
            if(carInfoDaHuaDTO1!=null){
                authAccountService.save(vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId(),updateVehicleCarBO.getOwnerAccountId(),
                        carInfoDaHuaDTO1.getOwnerId(),carInfoDaHuaDTO1.getOwnerCode(),vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId());
                if(vehicleCard.getCardType()==1){
                    CardMonthChargeDaHuaDTO cardMonthChargeDaHuaDTO = updateVehicleCarBOtoCardMonthChargeDaHuaDTO(carInfoDaHuaDTO1.getOwnerId(), updateVehicleCarBO, vehicleRelationCarToCard,vehicleCard);
                    chargeMonthCard(vehicleIntegratedConfigBO,cardMonthChargeDaHuaDTO);
                }
            }
        }

        //查看是否开启了蒙牛红门的双厂商定制化需求
        if(vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getCustomParams() != null){
            JSONObject customParamsObj = JSONObject.parseObject(vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getCustomParams());
            if(customParamsObj.getBoolean("doubleFunContrMn")){
                this.updateHongMenCar(vehicleIntegratedConfigBO, updateVehicleCarBO, customParamsObj);
            }
        }
    }

    /**
     * 蒙牛红门的更新车辆信息，定制化需求
     */
    public void updateHongMenCar(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, UpdateVehicleCarBO updateVehicleCarBO, JSONObject customParamsObj){
        VehicleCar vehicleCar = vehicleCarService.getById(updateVehicleCarBO.getId());
        //查看是否是修改了车牌号，如果是修改了，就授权，否则，就不授权
        if(!StringUtils.equals(vehicleCar.getPlateNumber(), updateVehicleCarBO.getPlateNumber())){
            List<VehicleRelationCarToCard> vehicleRelationCarToCardList = vehicleRelationCarToCardService.list(conditionForCarCard(updateVehicleCarBO.getId()));
            //将原来的车牌号的通行证去掉
//            hongMenHttpService.deleteCar(customParamsObj.getString("host"), vehicleCar.getPlateNumber());
            this.deleteHongMenCar(vehicleIntegratedConfigBO, updateVehicleCarBO.getId(), customParamsObj);
            //对更改后的车牌号进行授权
            hongMenHttpService.addCar(customParamsObj.getString("host"), updateVehicleCarBO.getPlateNumber());
            hongMenHttpService.editCarTime(customParamsObj.getString("host"), updateVehicleCarBO.getPlateNumber(), vehicleRelationCarToCardList.get(0).getValidEndTime());
        }
    }

    @Override
    public void deleteCar(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, String carDataStr) {
        DeleteVehicleCarBO deleteVehicleCarBO = JSONObject.parseObject(carDataStr, DeleteVehicleCarBO.class);
        VehicleCar vehicleCar = vehicleCarService.getById(deleteVehicleCarBO.getId());
        //获取大华的车辆信息，找出需要删除的车辆的第三方id
        List<CarInfoDaHuaDTO> daHuaCarList = daHuaHttpService.getDaHuaCarList(vehicleIntegratedConfigBO);
        if(daHuaCarList==null){
            return;
        }
        CarInfoDaHuaDTO carInfoDaHuaDTO = daHuaCarList.stream().filter(s -> {
            if (StringUtils.equals(s.getCarNum(), vehicleCar.getPlateNumber())) {
                return true;
            } else {
                return false;
            }
        }).findAny().orElse(null);
        if(carInfoDaHuaDTO!=null){
            //根据第三方车id删除车辆信息
            daHuaHttpService.deleteCar(vehicleIntegratedConfigBO, carInfoDaHuaDTO.getCarId());
        }

        //查看是否开启了蒙牛红门的双厂商定制化需求
        if(vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getCustomParams() != null){
            JSONObject customParamsObj = JSONObject.parseObject(vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getCustomParams());
            if(customParamsObj.getBoolean("doubleFunContrMn")){
                this.deleteHongMenCar(vehicleIntegratedConfigBO, deleteVehicleCarBO.getId(), customParamsObj);
            }
        }
    }

    /**
     * 蒙牛红门的删除车辆信息，定制化需求
     */
    public void deleteHongMenCar(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, Integer carId, JSONObject customParamsObj){
        VehicleCar vehicleCar = vehicleCarService.getById(carId);
        //先查看红门中是否有该车牌信息
        JSONArray carArr = hongMenHttpService.queryCarByPlateNumber(customParamsObj.getString("host"), vehicleCar.getPlateNumber());
        if(carArr == null || carArr.size() == 0){
            return;
        }
        hongMenHttpService.deleteCar(customParamsObj.getString("host"), vehicleCar.getPlateNumber());
    }

    /**
     * 建立所有场区的大华DTO类
     */
    private List<ParkingLotDto> getParkingLotDTO(Integer parkId){
        List<VehicleArea> vehicleAreas = vehicleAreaService.list(conditionForArea(parkId));
        List<ParkingLotDto> parkingLotDtos = new LinkedList<>();
        for (VehicleArea vehicleArea : vehicleAreas) {
            ParkingLotDto parkingLotDto = new ParkingLotDto();
            parkingLotDto.setParkingLot(vehicleArea.getAreaName());
            parkingLotDto.setParkingLotCode(vehicleArea.getRemark());
            parkingLotDto.setTotalParkCur(totalParkCur);
            parkingLotDto.setParkingLotId(vehicleArea.getThirdAreaId());
            parkingLotDtos.add(parkingLotDto);
        }
        return parkingLotDtos;
    }

    /**
     * 中台的车辆类型，转换为大华的车辆类型
     */
    private String convertForDaHuaCarType(Integer carType){
        switch (carType){
            case 1:
                return "1";
            case 2:
                return "2";
            case 3:
                return "12";
            case 5:
                return "13";
            default:
                return "0";
        }
    }

    /**
     * 中台的车牌颜色，转换为大华的车牌颜色
     */
    private String convertForDaHuaCarNumColor(String carNumColor){
        if("蓝色".equals(carNumColor)){
            return "0";
        }else if("绿色".equals(carNumColor)){
            return "4";
        }else if("黄色".equals(carNumColor)){
            return "1";
        }else if("黑色".equals(carNumColor)){
            return "3";
        }else if("白色".equals(carNumColor)){
            return "2";
        }else{
            return "100";
        }
    }

    /**
     * 中台的车卡类型，转换为大华的车卡类型
     */
    private String convertForDaHuaCardType(Integer cardType, Integer projectId){
//        switch (cardType){
//            case 0:
//                return "3";
//            case 1:
//                return "2";
//            case 2:
//                return "1";
//            case 3:
//                return "0";
//            default:
//                return null;
//        }
        //由于多套大华ICC平台中的用户编码不一致，所以不能使用枚举的方式来做映射
        LambdaQueryWrapper<VehicleCard> vehicleCardLambdaQueryWrapper = new LambdaQueryWrapper<>();
        vehicleCardLambdaQueryWrapper.eq(VehicleCard::getCardType, cardType)
                .eq(VehicleCard::getProjectId, projectId);
        List<VehicleCard> vehicleCardList = vehicleCardService.list(vehicleCardLambdaQueryWrapper);
        if(vehicleCardList.isEmpty()){
            throw new BusinessException("未找到该卡类型的信息");
        }
        return vehicleCardList.get(0).getThirdRuleId();
    }

    /**
     * 月卡缴费
     */
    private void chargeMonthCard(VehicleIntegratedConfigBO vehicleIntegratedConfigBO,CardMonthChargeDaHuaDTO cardMonthChargeDaHuaDTO){
        daHuaHttpService.chargeMonthCard(vehicleIntegratedConfigBO,cardMonthChargeDaHuaDTO);
    }

    /**
     * 新增车辆类生成大华的月卡缴费的实体类
     */
    private CardMonthChargeDaHuaDTO createVehicleCarBOtoCardMonthChargeDaHuaDTO(String openId,CreateVehicleCarBO createVehicleCarBO,VehicleCard vehicleCard){
        CardMonthChargeDaHuaDTO cardMonthChargeDaHuaDTO = new CardMonthChargeDaHuaDTO();
        cardMonthChargeDaHuaDTO.setOwnerId(openId);
        cardMonthChargeDaHuaDTO.setOwner(createVehicleCarBO.getOwnerName());
        cardMonthChargeDaHuaDTO.setCardNumber(cardNumber);
        cardMonthChargeDaHuaDTO.setStartTime(AboutDateUtil.dateToString(createVehicleCarBO.getValidStartTime()));
        if(createVehicleCarBO.getValidEndTime()==null){
            cardMonthChargeDaHuaDTO.setEndTime(AboutDateUtil.dateToString(createVehicleCarBO.getValidStartTime()));
        }else{
            cardMonthChargeDaHuaDTO.setEndTime(AboutDateUtil.dateToString(createVehicleCarBO.getValidEndTime()));
        }
        cardMonthChargeDaHuaDTO.setMoney(money);
        cardMonthChargeDaHuaDTO.setRechargeTimeStr(AboutDateUtil.dateToString(new Date()));
        cardMonthChargeDaHuaDTO.setOwnerTypeId(vehicleCard.getThirdRuleId());
        cardMonthChargeDaHuaDTO.setOwnerType(vehicleCard.getName());
        return cardMonthChargeDaHuaDTO;
    }

    /**
     * 更新车辆类生成大华的月卡缴费的实体类
     */
    private CardMonthChargeDaHuaDTO updateVehicleCarBOtoCardMonthChargeDaHuaDTO(String openId,UpdateVehicleCarBO updateVehicleCarBO,VehicleRelationCarToCard vehicleRelationCarToCard,VehicleCard vehicleCard){
        CardMonthChargeDaHuaDTO cardMonthChargeDaHuaDTO = new CardMonthChargeDaHuaDTO();
        cardMonthChargeDaHuaDTO.setOwnerId(openId);
        cardMonthChargeDaHuaDTO.setOwner(updateVehicleCarBO.getOwnerName());
        cardMonthChargeDaHuaDTO.setCardNumber(cardNumber);
        cardMonthChargeDaHuaDTO.setStartTime(AboutDateUtil.dateToString(vehicleRelationCarToCard.getValidStartTime()));
        if(vehicleRelationCarToCard.getValidEndTime()==null){
            cardMonthChargeDaHuaDTO.setEndTime(AboutDateUtil.dateToString(vehicleRelationCarToCard.getValidStartTime()));
        }else{
            cardMonthChargeDaHuaDTO.setEndTime(AboutDateUtil.dateToString(vehicleRelationCarToCard.getValidEndTime()));
        }
        cardMonthChargeDaHuaDTO.setMoney(money);
        cardMonthChargeDaHuaDTO.setRechargeTimeStr(AboutDateUtil.dateToString(new Date()));
        cardMonthChargeDaHuaDTO.setOwnerTypeId(vehicleCard.getThirdRuleId());
        cardMonthChargeDaHuaDTO.setOwnerType(vehicleCard.getName());
        return cardMonthChargeDaHuaDTO;
    }

    /**
     * 为没有传有效期的免费卡赋值100年的有效期
     */
    private void setFreeCardTime(CreateVehicleCarBO createVehicleCarBO){
        VehicleCard vehicleCard = vehicleCardService.getById(createVehicleCarBO.getCardId());
        if(vehicleCard.getCardType().equals(VehicleCardTypeEnum.FREE.getId())){
            if(createVehicleCarBO.getValidStartTime() == null){
                createVehicleCarBO.setValidStartTime(new Date());
            }
            if(createVehicleCarBO.getValidEndTime() == null){
                createVehicleCarBO.setValidEndTime(AboutDateUtil.datePlusYear(createVehicleCarBO.getValidStartTime(), 100));
            }
        }
    }
}
